<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>照片资源管理</title>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <style>
    body { font-family: "微软雅黑", sans-serif; padding: 20px; max-width: 1200px; margin: 0 auto; }
    .container { display: flex; flex-direction: column; gap: 15px; }
    .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }
    h2 { margin: 0; color: #2c3e50; }
    ul { list-style: none; padding-left: 20px; margin: 10px 0; }
    .item-container { display: flex; justify-content: space-between; align-items: center; width: 100%; margin-bottom: 5px; white-space: nowrap; }
    .item-name { flex: 1; display: flex; align-items: center; gap: 8px; overflow: hidden; text-overflow: ellipsis; }
    .folder { font-weight: bold; cursor: pointer; color: #1a73e8; }
    .file { color: #333; }
    .item-controls { display: flex; gap: 5px; align-items: center; }
    .item-controls button { padding: 3px 8px; font-size: 12px; background-color: #1a73e8; color: white; border: none; border-radius: 4px; cursor: pointer; white-space: nowrap; }
    .item-controls button:hover { background-color: #1557b0; }
    li { display: flex; align-items: center; width: 100%; }
    .controls { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 15px; padding: 10px; background-color: #f5f7fa; border-radius: 6px; align-items: center; }
    .controls button { padding: 6px 12px; background-color: #1a73e8; color: white; border: none; border-radius: 4px; cursor: pointer; transition: all 0.2s ease; display: flex; align-items: center; gap: 5px; }
    .controls button:hover { background-color: #1557b0; transform: translateY(-1px); box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
    .path-bar { margin-bottom: 5px; color: #666; font-size: 14px; padding: 8px 10px; background-color: #f8f9fa; border-radius: 4px; }
    #tree { border: 1px solid #e0e0e0; border-radius: 6px; padding: 15px; min-height: 300px; background-color: white; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
    .folder:hover, .file:hover { background-color: #f0f7ff; border-radius: 3px; padding-left: 5px; margin-left: -5px; }
    .controls button:active { transform: translateY(0); }
    @media (max-width: 768px) { .header { flex-direction: column; align-items: flex-start; gap: 10px; } }
  </style>
</head>
<body>
  <div class="container">
    <div class="header">
      <h2>📁 照片资源管理 V1.0</h2>
      <div class="path-bar" id="pathBar">当前路径：/</div>
    </div>
    <div class="controls">
      <button onclick="goBack()">🔙 返回上一级</button>
      <input type="file" id="fileInput" style="display: none" onchange="uploadFile(event)">
      <button onclick="triggerUpload()">📤 上传文件</button>
      <button onclick="createFolder()">📂 新建文件夹</button>
    </div>
    <div id="tree"></div>
  </div>

  <script>
    const ip = window.location.hostname;  //'127.0.0.1';
    const baseTreeApi = `http://${ip}:8080/api/tree`;
    const basePhotoUrl = `http://${ip}:8080/photos`;
    const deleteApi = `http://${ip}:8080/api/delete`;
    const renameApi = `http://${ip}:8080/api/rename`;
    const uploadApi = `http://${ip}:8080/api/upload`;
	const createFolderApi = `http://${ip}:8080/api/create-folder`;
    let currentPath = '';

    function encodePath(path) {
      return path.split('/').map(encodeURIComponent).join('/');
    }

	function createFolder() {
	  const folderName = prompt('请输入新文件夹名称：');
	  if (!folderName) return;

	  axios.post(createFolderApi, {
		parentPath: currentPath,
		folderName: folderName
	  }).then(() => {
		alert('文件夹创建成功');
		loadTree(currentPath);
	  }).catch(err => alert('创建失败：' + err));
	}

    function loadTree(path = '') {
      currentPath = path;
      document.getElementById('pathBar').innerText = `当前路径：/${decodeURIComponent(path)}`;
      axios.get(`${baseTreeApi}?path=${encodePath(path)}`).then(res => {
        const folders = res.data.folders || [];
        const files = res.data.files || [];

        const tree = document.getElementById('tree');
        tree.innerHTML = '';
        const ul = document.createElement('ul');

        folders.forEach(folder => {
          const li = document.createElement('li');
          li.innerHTML = `<div class="item-container"><span class="item-name"><span class="folder" onclick="loadTree('${folder.path}')">📁 ${folder.name}</span></span><span class="item-controls"><button onclick="renameItem('${folder.path}')">重命名</button><button onclick="deleteItem('${folder.path}')">删除</button></span></div>`;
          ul.appendChild(li);
        });

        files.forEach(file => {
          const li = document.createElement('li');
          li.innerHTML = `
                        <div class="item-container">
                          <span class="item-name">
                            <span class="file">
                              🖼️ <a href="${basePhotoUrl}/${encodePath(file.path)}" target="_blank">${file.name}</a>
                            </span>
                          </span>
                          <span class="item-controls">
                            <button onclick="renameItem('${file.path}')">重命名</button>
                            <button onclick="deleteItem('${file.path}')">删除</button>
                          </span>
                        </div>`;
                              ul.appendChild(li);
        });
        tree.appendChild(ul);
      });
    }

    function goBack() {
      if (!currentPath) return;
      const parts = currentPath.split('/');
      parts.pop();
      loadTree(parts.join('/'));
    }

    function renameItem(oldPath) {
      const name = prompt('请输入新名称：');
      if (!name) return;
      axios.post(renameApi, { oldPath, newName: name }).then(() => {
        alert('重命名成功');
        loadTree(currentPath);
      }).catch(err => alert('重命名失败：' + err));
    }

    function deleteItem(path) {
      if (!confirm(`确认删除 ${path} 吗？`)) return;
      axios.delete(deleteApi, { data: { path } }).then(() => {
        alert('删除成功');
        loadTree(currentPath);
      }).catch(err => alert('删除失败：' + err));
    }

    function triggerUpload() {
      document.getElementById('fileInput').click();
    }

    function uploadFile(event) {
      const file = event.target.files[0];
      if (!file) return;

      const formData = new FormData();
      formData.append('file', file);
      formData.append('targetDir', currentPath);

      axios.post(uploadApi, formData).then(() => {
        alert('上传成功');
        loadTree(currentPath);
      }).catch(err => alert('上传失败：' + err));
    }

    loadTree();
  </script>
</body>
</html>
